package com.example.arve;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Description:
 * Just displays a youtube video based on what the user typed in as the URL 
 * (Technically they can get to any web page at the moment, but obviously this is just for testing purposes
 * to make sure that internet functionality is indeed working)
 * 
 * What's working:
 * - A webpage is loaded up based on the users entered URL
 * What's not:
 * - Whenever the user leaves the activity, the web page is sent to a dead link so that the video doesn't continue playing.
 *   I'd like to get it to simply pause the video, but I'm not sure if that's possible without the youtube API and I'm
 *   under the impression it requires a fairly high android version. I could be wrong though, so I'll be looking into it
 *   in the future
 * @author Richard
 *
 */
public class YoutubeActivity extends ActionBarActivity 
{
	private WebView main_page;
	private String current_url;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);

		//Set our content view so we can grab the web view
		setContentView(R.layout.activity_youtube);
		
		//Retrieve our intent
		Intent my_intent = getIntent();
		//And get the url provided
		current_url = my_intent.getStringExtra(BaseActivity.YOUTUBE_URL_MESSAGE);
		
		//Grab the web view
		main_page = (WebView)findViewById(R.id.web_view);
		//Setup a web client
		main_page.setWebViewClient(new WebViewClient());
		//Get something to play around with the settings
		WebSettings web_settings = main_page.getSettings();
		//Enable javascript
		web_settings.setJavaScriptEnabled(true);
		//And videos (Deprecated so not sure how relevant it'll be for later versions of android
		web_settings.setPluginState(WebSettings.PluginState.ON);
		//And load up the page
		main_page.loadUrl(current_url); 
		
		
		
		
		/*if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}*/
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.youtube, menu);
		return true;
	}
	
	@Override
	public void onPause()
	{
		super.onPause();
		//Load a non-existant URL while our user isn't looking. This way the youtube video isn't continuing while they're
		//not watching it. Very cheap trick; needs an actual fix but it works for now. Thanks StackOverflow!
		main_page.loadUrl("file:///deadlink.html");
		main_page.stopLoading();
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		//Meant to reload the youtube video when we go back into the app; doesn't seem to do that. Probably missing 
		//something here 
		main_page.loadUrl(current_url);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment 
	{

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_youtube,
					container, false);
			return rootView;
		}
	}

}
